package com.chengjj.kings.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: 成俊杰
 * @CreateDate: 2019/3/7
 * @Version: 1.0
 * @Description: swagger2配置类
 *
 * swagger会管理所有包含@ApiOperation注解的控制器方法，同时，可利用@ApiImplicitParams注解标记接口中的参数，具体用法请参考CodeController类中的用法。
 * ```
 *  @ApiOperation("生成代码")
 *  @ApiImplicitParams({
 *          @ApiImplicitParam(name = "moduleName", value = "模块名称", required = true, dataType = "String"),
 *          @ApiImplicitParam(name = "bizChName", value = "业务名称", required = true, dataType = "String"),
 *          @ApiImplicitParam(name = "bizEnName", value = "业务英文名称", required = true, dataType = "String"),
 *          @ApiImplicitParam(name = "path", value = "项目生成类路径", required = true, dataType = "String")
 *  })
 *  @RequestMapping(value = "/generate", method = RequestMethod.POST)
 * ```
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "kings", name = "swagger-open", havingValue = "true")
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //这里采用包含注解的方式来确定要显示的接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //这里采用包扫描的方式来确定要显示的接口
                //.apis(RequestHandlerSelectors.basePackage("com.chengjj.controller..."))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Kings_Project DOC")
                .description("Kings_Project Api文档")
                //服务条款网址
                .termsOfServiceUrl("https://gitee.com/chengjunjie/kings")
                .contact(new Contact("kings", "https://gitee.com/chengjunjie/kings", "chengjunjie_dev@foxmail.com"))
                .version("1.0")
                .build();
    }
}
